package com.it.mybatisplus.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.mybatisplus.Mapper.UserMapper;
import com.it.mybatisplus.entity.User;
import com.it.mybatisplus.tools.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 羡羡
 * @Date: 2022/05/25/09:40
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserMapper us;

    /**
     * 查询所有的数据
     * @return
     */
    @Override
    public JsonResult seall() {
        List<User> lis=us.selectList(null);
        return new JsonResult(200,"获取成功！",lis);
    }

    /**
     * 查询指定ID的数据
     * @param id
     * @return
     */
    @Override
    public JsonResult seonuser(int id) {
        User usenti=us.selectById(id);
        return new JsonResult(200,"获取成功！",usenti);
    }

    /**
     * 添加一条记录
     * @param usr
     * @return
     */
    @Override
    public JsonResult addone(User usr) {
        int adsu=us.insert(usr);
        if(adsu>0){
            return new JsonResult(200,"添加成功！");
        }else{
            return new JsonResult(500,"添加失败！");
        }
    }

    /**
     * 删除一条
     * @param id
     * @return
     */
    @Override
    public JsonResult delone(int id) {
        int desu=us.deleteById(id);
        if(desu>0){
            return new JsonResult(200,"删除成功！");
        }else{
            return new JsonResult(500,"删除失败！");
        }
    }

    /**
     * 修改数据
     * @param usr
     * @return
     */
    @Override
    public JsonResult upone(User usr) {
        int upsu=us.update(usr,null);
        if(upsu>0){
            return new JsonResult(200,"更新成功！");
        }else{
            return new JsonResult(500,"更新失败！");
        }
    }

    /**
     * 统计数量
     * @return
     */
    @Override
    public JsonResult counnumber() {
        int coun= Math.toIntExact(us.selectCount(null));
        return new JsonResult(200,"获取成功！",coun);
    }

    /**
     * like查询
     * @param name
     * @return
     */
    @Override
    public JsonResult likelist(String name) {
        QueryWrapper<User> qu=new QueryWrapper<>();
        qu.like("name",name);
        List<User> lis=us.selectList(qu);
        return new JsonResult(200,"查询成功！",lis);
    }
}
